/*
 * Package: org.grassfield.common.service
 * FileName: UserService.java
 *  
 * Created by pandian on Jan 28, 2011
 */
package service;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.apache.log4j.Logger;
import dao.IUserDAO;
import bean.User;


/**
 * The Class UserService.
 */
public class UserService implements IUserService {
	Logger logger = Logger.getLogger(UserService.class);
	private IUserDAO userDAO;

	/**
	 * Sets the user dao.
	 * 
	 * @param userDAO
	 *            the new user dao
	 */
	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}

	@Override
	public User getUser(String userName, String password) {
		return userDAO.getUser(userName, password);
	}

	@Override
	public User getUser(long id) {
		return this.userDAO.getUser(id);
	}

	@Override
	public List<User> list() {
		return this.userDAO.list();
	}

	@Override
	public User addUser(User user) {
		return this.userDAO.addUser(user);
	}

	@Override
	public Set<User> getIdAryToSet(Long[] userIdAry) {
		Set<User> result = new TreeSet<User>();
		if (userIdAry != null) {
			for (Long id : userIdAry) {
				User user = this.getUser(id);
				if (user != null) {
					result.add(user);
				} else
					logger.error("user is null for the id " + id);
			}
		}
		return result;
	}

	@Override
	public Long[] getAryToUserSet(Set<User> userList) {
		if (userList == null)
			return new Long[] {};

		Long[] result = new Long[userList.size()];
		logger.debug("userlist size:" + result.length);
		int c = 0;
		for (User user : userList) {
			result[c] = user.getId();
			logger.debug("user iterate:" + user.getId() + " "
					+ user.getUserName());
			logger.debug("result[c]:" + result[c]);
			c++;
		}
		return result;
	}

}
